package com.huang.leetcode.doublePointer;

import java.util.Arrays;

/**
 * @Author：CreateSequence
 * @Date：2020-07-28 14:15
 * @Description：移动零
 * 题解：https://leetcode-cn.com/problems/move-zeroes/solution/dong-hua-yan-shi-283yi-dong-ling-by-wang_ni_ma/
 */
public class PB283 {

    public void moveZeroes(int[] nums) {

        //第一遍遍历记录0元素的个数，并且将非0的元素按顺序移动到数组前端
        int i = 0;
        for (int j = 0; j < nums.length; j++) {
            if (nums[j] != 0) {
                nums[i++] = nums[j];
            }
        }

        //第二遍遍历把末尾元素全变为0
        for (int j = i; j < nums.length; j++) {
            nums[j] = 0;
        }
    }

}
